<?php
/********************************
 *  描述: 简单日历类
 *  作者: fenghao
 *  创建: 2012-11-14
 ********************************/

class SimpleCalendar{
	public $sTimeStamp;				//时间戳
	private $aDate;
	private $sDate;					//时间日期

    //可是使用时间戳或者字符串初始化，如果参数为空，返回当前时间
	function __construct($ts=NULL){
		if(is_string($ts)) $ts = strtotime($ts);
        if($ts == NULL)  $ts = time();
		$this->sTimeStamp = $ts;
        $this->aDate = getdate($ts);
        $this->sDate = date("Y-m-d H:i:s",$ts);
    }
    function __destruct(){
        $this->sTimeStamp = NULL;
        $this->aDate = NULL;
        $this->sDate = NULL;
    }
	
	function debug(){
		print "<p>TimeStamp is {$this->sTimeStamp}</p>\n";
		print "<p>Data's array is (";
		foreach ($this->aDate as $key=>$value)
			print "{$key} => {$value}, ";
		print ")</p>\n";
		print "<p>Data string is {$this->sDate}</p>\n";
	}
	//重新设置日期和时间
	function setDate($ts=NULL){
		if(is_string($ts)) $ts = strtotime($ts);
        if($ts == NULL)  $ts = time();
		$this->sTimeStamp = $ts;
        $this->aDate = getdate($ts);
        $this->sDate = date("Y-m-d H:i:s",$ts);
    }
	//取得当前日期所在周的周一，可以返回日期字符串或者时间戳
	function getWeekStart($rts=0){
		$wday = $this->aDate['wday'];
		if($wday){
			$ws = mktime(0,0,0,$this->aDate['mon'],($this->aDate['mday'] - $wday + 1 ),$this->aDate['year']);
		}else{
			$ws = mktime(0,0,0,$this->aDate['mon'],($this->aDate['mday'] - 6),$this->aDate['year']);
		}
		if($rts){
			return $ws;
		}else{
			return date("Y-m-d", $ws);
		}
	}
	//取得当前日期所在周的周日，可以返回日期字符串或者时间戳
	function getWeekEnd($rts=0){
		$wday = $this->aDate['wday'];
		if($wday){
			$ws = mktime(0,0,0,$this->aDate['mon'],($this->aDate['mday'] - $wday + 7 ),$this->aDate['year']);
		}else{
			$ws = mktime(0,0,0,$this->aDate['mon'],($this->aDate['mday']),$this->aDate['year']);
		}
		if($rts){
			return $ws;
		}else{
			return date("Y-m-d", $ws);
		}
	}
	//取得当前日期所在周的所有天，$startday 为起始时间，0为周日，1为周一，依次
	function getWeek($startday=0){
		$wday = $this->aDate['wday'];
		if($wday){
			for($i=0;$i<7;$i++)
				$week[] = date("Y-m-d",mktime(0,0,0,$this->aDate['mon'],($this->aDate['mday'] - $wday + $startday + $i),$this->aDate['year']));
		}else{
			for($i=0;$i<7;$i++)
				$week[] = date("Y-m-d",mktime(0,0,0,$this->aDate['mon'],($this->aDate['mday'] - 7 + $startday + $i),$this->aDate['year']));
		}
		return $week;
	}
	//取得当前月份的天数
	function getMonth(){
		//$curr = $this->aDate['year']."-".$this->aDate['mon']."-".$this->aDate['mday'];
		$next = $this->aDate['year']."-".($this->aDate['mon']+1)."-".$this->aDate['mday'];
		//echo is_string($next);
		return abs($this->getDiffOf2Day($next));
	}
	//增加时间，默认返回时间戳，如果需要返回字符串，将$rts设为0
	function addDate($day=0, $mon=0, $year=0, $hour=0, $minute=0, $second=0, $rts=1){
		$ts = mktime(($this->aDate['hours']+$hour),($this->aDate['minutes']+$minute),($this->aDate['seconds']+$second),($this->aDate['mon']+$mon),($this->aDate['mday']+$day),($this->aDate['year']+$year));
		return $rts?$ts:date("Y-m-d H:i:s", $ts);
	} 
	//取得两天之间相差的天数；默认取得给定日期和当前日期间相差的天数
	function getDiffOf2Day($from, $to=NULL){
		if(is_string($from)) { $from = strtotime($from);}
		if($to == NULL){ $to = $this->sTimeStamp; }
		elseif(is_string($to)){ $to = strtotime($to); }
		$from = mktime(0,0,0,date("m",$from),date("d",$from),date("Y",$from)); 
		$to = mktime(0,0,0,date("m",$to),date("d",$to),date("Y",$to)); 
		return ($from - $to)/86400;
	}
	//取得给定的两个时间之间相差的毫秒数
	function getDiffOf2TimeStamp($from, $to=NULL){
		if(is_string($from)) { $from = strtotime($from);}
		if($to == NULL){ $to = $this->sTimeStamp; }
		elseif(is_string($to)){ $to = strtotime($to); }
		return $from - $to;
	}
	//取得指定日期、指定周数的数组
	function getWeekDays($weeks=3, $start_day = 0){
		$week = NULL;
		$i=0;		
		$week[$i++] = $this->getWeek($start_day);
		for(; $i<$weeks; $i++){
			$this->setDate($this->addDate(7));		
			$week[$i] = $this->getWeek($start_day);
		}
		return $week;
	}
}


?>